﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MyCommerce.Presentation.UsersAndSecurity.IViews;
using Innova.Microfinanzas.BusinessEntities;
using Innova.Microfinanzas.Business.UsersAndSecurity;

namespace MyCommerce.Presentation.UsersAndSecurity.Presenters
{
    public class CommandInRolePresenter
    {
        private ICommandInRoleView pViewToPresent;

        public CommandInRolePresenter(ICommandInRoleView vViewToPresent)
        {
            pViewToPresent = vViewToPresent;
            pViewToPresent.OnCancel += new EventHandler(pViewToPresent_OnCancel);
            pViewToPresent.OnLoadView += new EventHandler(pViewToPresent_OnLoadView);
            pViewToPresent.OnSave += new EventHandler(pViewToPresent_OnSave);
        }

        private void pViewToPresent_OnSave(object sender, EventArgs e)
        {
            UserManager userMgr = new UserManager();
            userMgr.UpdateCommandForRole(pViewToPresent.IdRoleLoad, pViewToPresent.SelectedCommands);
            pViewToPresent.CloseAndSetResult(true);
        }

        private void pViewToPresent_OnLoadView(object sender, EventArgs e)
        {
            UserManager userMgr = new UserManager();
            List<Command> colCommands = userMgr.GetAllCommands();
            if (colCommands == null)
                colCommands = new List<Command>();
            pViewToPresent.ColCommandsLoaded = ConvertFromDbCommans(colCommands);
            pViewToPresent.SelectedCommands = userMgr.GetCommandsInRole(pViewToPresent.IdRoleLoad);
        }

        private void pViewToPresent_OnCancel(object sender, EventArgs e)
        {
            pViewToPresent.CloseAndSetResult(false);
        }

        private List<Command> ConvertFromDbCommans(List<Command> commands)
        {
            List<Command> colCommandsToLoad = new List<Command>(commands);
            List<Command> roots = GetRootCommands(colCommandsToLoad);
            List<Command> nodes = new List<Command>();
            foreach (Command root in roots)
            {
                root.ColChildCommands = AddCommandToTreeNode(colCommandsToLoad, root);
                nodes.Add(root);
            }
            return nodes;
        }

        private List<Command> AddCommandToTreeNode(List<Command> commands, Command root)
        {
            List<Command> treeCommand = new List<Command>();
            if (treeCommand == null)
                treeCommand = new List<Command>();
            List<Command> children = GetChildCommans(root, commands);
            foreach (Command child in children)
            {   
                List<Command> children1 = GetChildCommans(child, commands);
                if (children1.Count > 0)
                {
                    child.ColChildCommands = AddCommandToTreeNode(commands, child);
                }
                treeCommand.Add(child);
            }
            return treeCommand;
        }

        private List<Command> GetChildCommans(Command parent, List<Command> commands)
        {
            List<Command> children = new List<Command>();
            foreach (Command command in commands)
            {
                if (command.Parent == parent.IdCommand)
                {
                    children.Add(command);
                }
            }
            children.Sort((objComp1, objComp2) => objComp1.OrderBy.CompareTo(objComp2.OrderBy));
            return children;
        }

        private List<Command> GetRootCommands(List<Command> commands)
        {
            List<Command> roots = new List<Command>();
            foreach (Command command in commands)
            {
                if (command.Parent == 0)
                {
                    roots.Add(command);
                }
            }

            roots.Sort((objComp1, objComp2) => objComp1.OrderBy.CompareTo(objComp2.OrderBy));
            return roots;
        }
    }
}
